import 'dart:io';

import 'package:super_study_english_tool/dao/dao_user_album.dart';
import 'package:super_study_english_tool/dao/dao_user_album_media.dart';
import 'package:super_study_english_tool/dao/dao_user_album_media_record.dart';
import 'package:super_study_english_tool/model/model_user_album_media.dart';
import 'package:super_study_english_tool/tools/tools_subtitle.dart';

import '../model/model_user_album.dart';

class ServiceUserAlbum {
  static Future<bool> addUserAlbum(String path) async {
    print("addUserAlbum()");

    ModelUserAlbum m = new ModelUserAlbum();
    m.name = path.substring(path.lastIndexOf("/") + 1);
    m.filePath = path;

    // 判断是否已存在
    ModelUserAlbum? m3 = DaoUserAlbum.findByPath(path);
    if (m3 != null) {
      print("已存在,不添加");
      return false;
    }

    // 遍历子文件
    Stream<FileSystemEntity> fileList = Directory(path).list();
    List<ModelUserAlbumMedia> ms = [];
    Set<String> wordSetAll = new Set();
    num speedAll = 0;
    int sentenceQtyAll = 0;
    int mediaQty = 0;
    await for (FileSystemEntity fileSystemEntity in fileList) {
      print('$fileSystemEntity');
      ModelUserAlbumMedia muam = new ModelUserAlbumMedia();
      muam.filePath = fileSystemEntity.path;
      muam.fileName = muam.filePath!.substring(muam.filePath!.lastIndexOf("/") + 1);
      String subName = muam.fileName!.substring(muam.fileName!.lastIndexOf("."));
      if (subName == ".mp4" || subName == ".mp3") {
        print(muam);
        mediaQty++;

        String srtPath = muam.filePath!.substring(0, muam.filePath!.lastIndexOf("/") + 1);
        srtPath += muam.fileName!.substring(0, muam.fileName!.lastIndexOf("."));
        srtPath += ".srt";
        print(srtPath);

        File srtFile = new File(srtPath);
        if (await srtFile.exists()) {
          try {
            String srtContent = srtFile.readAsStringSync();
            // print(srtContent);
            var tmpL = ToolsSubtitle.analysisSrtContent(srtContent);
            print(tmpL);
            muam.sentenceQty = tmpL[0];
            sentenceQtyAll += muam.sentenceQty!;
            muam.wordQty = tmpL[1];
            muam.speed = tmpL[2];

            for (String s in tmpL[3]) {
              wordSetAll.add(s);
            }
            // wordSetAll.addAll(tmpL[3]);
            speedAll += tmpL[2];
          } on Exception catch (err) {
            print("读取srt出错," + err.toString());
          }
        }

        ms.add(muam);
      }
    }

    m.mediaQty = mediaQty;
    if (ms.length > 0) {
      m.speed = speedAll ~/ ms.length;
    } else {
      m.speed = 0;
    }
    m.sentenceQty = sentenceQtyAll;
    m.wordQty = wordSetAll.length;

    DaoUserAlbum.insert(m);
    ModelUserAlbum? m2 = DaoUserAlbum.findByName(m.name!);

    if (m2 != null) {
      print(m2!.id);
    } else {
      print("没找到");
    }

    for (ModelUserAlbumMedia me in ms) {
      me.userAlbumId = m2!.id;
      DaoUserAlbumMedia.insert(me);
    }
    return true;
  }

  static List<ModelUserAlbum> list() {
    List<ModelUserAlbum> list = DaoUserAlbum.findAll();
    // for (int i = 0; i < list.length; i++) {
    //   ModelUserAlbum a = list[i];
    //   List<ModelUserAlbumMedia> mediaList = DaoUserAlbumMedia.findAllByAlbumId(a.id!);
    //   for (int j = 0; j < mediaList.length; j++) {
    //     ModelUserAlbumMedia m = mediaList[i];
    //   }
    // }
    return list;
  }

  static List<ModelUserAlbum> listLastStudy() {
    List<ModelUserAlbum> list = DaoUserAlbum.findAll();
    return list;
  }

  static List<ModelUserAlbum> listNeedReview() {
    List<ModelUserAlbum> list = DaoUserAlbum.findAll();
    return list;
  }

  static void deleteAlbumById(int id) {
    // 删除Album
    DaoUserAlbum.deleteById(id);
    // 删除Album下media
    DaoUserAlbumMedia.deleteByAlbumId(id);
    // 删除所有相关记录
    DaoUserAlbumMediaRecord.deleteByAlbumId(id);
  }
}
